package com.wql.test.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.wql.test.constant.Constant;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * <strong>
 * Desc: 字符集统一处理，有点问题PostRequestFilter如果是错误信息，就是iso8859-1，还不知道原因
 * </strong>
 * <p>Author: WeiQiLong<p>
 * Create Time:2019/8/18 15:18<p>
 */
@Component
public class EncodingFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext context = RequestContext.getCurrentContext();
        try {
            context.getRequest().setCharacterEncoding(Constant.ENCODING);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        context.getResponse().setContentType("text/html;charset="+Constant.ENCODING);
        context.getResponse().setCharacterEncoding(Constant.ENCODING);

        return null;
    }
}
